﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.Entity;
using Northwind.Model;
using System.Diagnostics;

namespace Northwind.Repositories.Entity
{
    public class EmployeesRepository : IEmployeesRepository
    {
        #region Fields
        private NorthwindContext _context = new NorthwindContext();
        #endregion

        #region IEmployeesRepository Members
        public void Add(ref Employee item)
        {
            _context.Employees.Add(item);
        }

        public void Delete(int key)
        {
             _context.Employees.Remove(_context.Employees.FirstOrDefault(e => e.Id == key));
        }

        public Employee FindByKey(int key)
        {
            return _context.Employees.FirstOrDefault(e => e.Id == key);
        }

        public IEnumerable<Employee> FindAll()
        {
            return _context.Employees;
        }

        public IEnumerable<Employee> FindRange(string sortingExpression, int startIndex, int count)
        {
            return _context.Employees
                    .OrderBy(sortingExpression)
                    .Skip(startIndex)
                    .Take(count);
        }

        public int GetCount()
        {
            return _context.Employees.Count();
        }

        public void SaveChanges()
        {
            _context.SaveChanges();
        }
        #endregion
    }
}
